import { Modal } from 'antd';
import { CreateModalList } from '@/biz-components/hoc-form/with-list';
import { xhrPostJson } from '@/services';
import { onFieldValueChange } from '@formily/core';
import { message } from 'antd';
import { useRef, useState } from 'react';
import { history } from "umi";
import api, { arcvBrwrLend, arcvBrwrGiveBack } from './services';

const WithListComp = CreateModalList({});

const ArchivesManageList = (props: any) => {
  const formRef = useRef();
  const { match } = props;
  const [refresh, setRefresh] = useState(false);

  return (
    <>
      <WithListComp
        match={match}
        history={history}
        reloadFlag={refresh}
        pageCode={'ArcvBrw_L'}
        pageApi={api.page}
        extraPrams={{
          status: 'COMPLETE'
        }}
        noModal
        noCache
        validator={{
          checkColBtns: (key, row) => {
            if (key === 'guihuan') {
              return row.arcvBrwStatus === 'LEND';
            }
            if (key === 'start') {
              return row.arcvBrwStatus === 'PENDUNG';
            }

            return true;
          }
        }}
        formConfigs={{
          effects: (form: any) => {
            formRef.current = form;
          },
        }}
        events={{
          tableButtonsEvents: {
            handleView: (key: any, row: any) => {
              history.push({
                pathname: '/invoice/arcvBrw/detail',
                query: {
                  code: row.code
                }
              });
            },
            handleGiveBack: (key: any, row: any) => {
              Modal.confirm({
                title: '确认归还？',
                onOk: () => {
                  arcvBrwrGiveBack({
                    code: row.code,
                    arcvCode: row.arcvCode
                  }).then((res: any) => {
                    if (res.success) {
                      message.success('归还成功');
                      setRefresh(!refresh);
                    }
                  })
                }
              })
            },
            handleLend: (key: any, row: any) => {
              Modal.confirm({
                title: '确认借出？',
                onOk: () => {
                  arcvBrwrLend({
                    code: row.code,
                    arcvCode: row.arcvCode
                  }).then((res: any) => {
                    if (res.success) {
                      message.success('借出成功');
                      setRefresh(!refresh);
                    }
                  })
                }
              })
            }
          },
        }}
      />
    </>
  );
};

export default ArchivesManageList;
